//
//  WelcomeViewController.m
//  OC-LBWeibo
//
//  Created by 李博 on 15/7/9.
//  Copyright © 2015年 libo. All rights reserved.
//

#import "WelcomeViewController.h"
#import "UserAccount.h"
#import "UIImageView+WebCache.h"


@interface WelcomeViewController ()

@property(nonatomic,weak)UILabel *messageLabel;
@property (nonatomic, weak)UIImageView *iconView;

@property(nonatomic,strong)NSLayoutConstraint *bottomConstraint;

@end

@implementation WelcomeViewController

- (void)loadView
{
    self.view = [[UIView alloc]init];
    UIImageView *backImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ad_background"]];
    [self.view addSubview: backImageView];
    
    //设置自动布局
    backImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView" : backImageView}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subView]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView" : backImageView}]];
    
    self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    self.bottomConstraint = [NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-160];
    [self.view addConstraint:self.bottomConstraint];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[subView(90)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView" : self.iconView}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subView(90)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView" : self.iconView}]];
    
    self.messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.bottomConstraint setConstant:-([UIScreen mainScreen].bounds.size.height - 200)];

    [UIView animateWithDuration:2 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            self.messageLabel.alpha = 1.0;
        } completion:^(BOOL finished) {
            [[NSNotificationCenter defaultCenter] postNotificationName:LBSwitchRootVCNotification object:@(YES)];
        }];
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置头像
    NSString *urlString = [UserAccount sharedUserAccount].avatar_large;
    if (urlString != nil) {
        [self.iconView sd_setImageWithURL:[NSURL URLWithString:urlString]];
    }
    
    
}

- (UIImageView *)iconView
{
    if (_iconView == nil) {
        UIImageView *iconView = [[UIImageView alloc]init];
        iconView.image = [UIImage imageNamed:@"avatar_default_big"];
        iconView.layer.masksToBounds = YES;
        iconView.layer.cornerRadius = 45;
        
        self.iconView = iconView;
        [self.view addSubview:iconView];
        
    }
    return _iconView;
}

- (UILabel *)messageLabel {
    
    if (_messageLabel == nil) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"欢迎回来";
        [label sizeToFit];
        label.alpha = 0;
        
        self.messageLabel = label;
        [self.view addSubview:label];
        
    }
    return _messageLabel;
}

@end
